

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layuiAdmin 角色管理 iframe 框</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../../layuiadmin/layui/css/layui.css" media="all">
</head>
<body>

  <div class="layui-form" lay-filter="layuiadmin-form-role" id="layuiadmin-form-role" style="padding: 20px 30px 0 0;">
    <form action="" id="tf"  method="post" class="form form-horizontal" enctype="multipart/form-data">
    <div class="layui-form-item">
      <label class="layui-form-label">标题</label>
      <div class="layui-input-block">
        <input type="text" name="title" id="title" lay-verify="required" autocomplete="off" class="layui-input" value="{$data['title']|default=''}">
      </div>
    </div>
    <br>
      <div class="layui-form-item">
        <label class="layui-form-label">副标题</label>
        <div class="layui-input-block">
          <input type="text" name="subtitle" id="subtitle" lay-verify="required" autocomplete="off" class="layui-input" value="{$data['subtitle']|default=''}">
        </div>
      </div>
      <br>
    <div class="layui-form-item">
      <label class="layui-form-label">图片</label>
      <div class="layui-input-block">
        {if isset($data['img_src'])}
        <img class="img" width="80" height="80" src="{$data['img_src']|default=''}"/>
        {else}
        <img class="img" width="80" height="80" style="display: none" src="{$data['img_src']|default=''}"/>
        {/if}
        <input type="file" class="imgInput" value="" id="img_src" placeholder="" name="img_src">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">正文</label>
      <div class="layui-input-block">
        <!-- 加载编辑器的容器 -->
        <script id="container" name="content" type="text/plain">{$data.content|default=''}</script>
        <!-- 配置文件 -->
        <script type="text/javascript" src="__ueditor__/ueditor.config.js"></script>
        <!-- 编辑器源码文件 -->
        <script type="text/javascript" src="__ueditor__/ueditor.all.js"></script>
        <!-- 实例化编辑器 -->
        <script type="text/javascript">
          var ue = UE.getEditor('container');
        </script>
      </div>
    </div>
    <div class="layui-form-item" align="right">
      <button class="layui-btn" lay-submit lay-filter="LAY-user-role-submit" id="LAY-user-role-submit" onClick="form_submit();">提交</button>
    </div>
    </form>
  </div>

  <script src="../../../../layuiadmin/layui/layui.js"></script>
  <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
  <script>

      $(".imgInput").change(function(){
          $(".img").attr("src",URL.createObjectURL($(this)[0].files[0]));
          $(".img").show();
      });

  layui.config({
    base: '../../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
    var $ = layui.$
    ,form = layui.form ;
  })
  function form_submit(){
    if(document.getElementById("img_src").value==""){
      layer.msg("你没有选择文件");
    }else{
      var formData= new FormData(document.getElementById("tf"));
      $.ajax({
        type: 'POST',
        url: "{:url('add')}",
        data: formData,
        dataType: 'json',
        success: function (data){
          console.log("hello ajax");
          if(data.code){
            layer.closeAll();layer.msg(data.msg);
            setTimeout(function(){
              location.href = "{:url('news/lists')}";
            },300)
          }else{
            layer.msg(data.msg);
          }
        }
      })
    }
  }
  </script>
</body>
</html>